package com.controller;

import java.util.List;

import com.server.ArticleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import com.beans.Article;
import com.beans.Grade;
import com.mapper.ArticleMapper;
import com.mapper.GradeMapper;
import com.mapper.ArticleGradeMapper;

import javax.annotation.Resource;

@Controller
@RequestMapping("/user")
public class ArticleController {
	private static final Logger logger = LoggerFactory.getLogger(ArticleController.class);
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private GradeMapper gradeMapper;

    @Autowired
    private ArticleGradeMapper articleGradeMapper;
    @Resource
    private ArticleService articleService;
    //全局的,用于转向某个页面的
    @GetMapping("/{article-add}")
    public String go(@PathVariable("article-add") String page) {
        return page;
    }
    @GetMapping("/content")
    public String getArticle(int id, Model model) {
        // 获取文章对象
        Article article = articleService.searchByArticleID(id);

        // 添加到模型
        model.addAttribute("article", article);

        return "article-content";
    }
    @GetMapping("/articleDetails")
    public String getArticle2(int id, Model model) {
        // 获取文章对象
        Article article = articleService.searchByArticleID(id);

        // 添加到模型
        model.addAttribute("article", article);

        return "artDetails";
    }
    @GetMapping("/articles")
    public String getAllArticles(Model model) {
        List<Article> articlelist = articleMapper.getAllArticles();
        model.addAttribute("articlelist", articlelist);
        logger.info("获取的文章数据：" + articlelist); // 使用日志记录数据
        return "article-manager";
    }
    @GetMapping("/article")
    public String getAllArticle(Model model) {
        List<Article> articlelist = articleMapper.getAllArticles();
        model.addAttribute("articlelist", articlelist);
        logger.info("获取的文章数据：" + articlelist); // 使用日志记录数据
        return "articleList";
    }
    // 处理添加文章的请求
    @PostMapping("/addArticle")
    public String addArticle(Article article, ModelMap m
            ) {
        articleService.addArticle(article);
        m.put("msg","文章添加成功");
        return "redirect:/user/articles";
    }
    //根据id查询用户信息,转到用户修改页面
    @GetMapping("/update")
    public String gotoUpdate(int id,ModelMap m ) {
        Article article = articleService.searchByArticleID(id);
        m.put("article", article);
        return "article-update";
    }

    //真正的提交,进行修改
    @PostMapping("/update")
    public String update(Article article,ModelMap m ) {
        articleService.updateArticle(article);
        m.put("article", article);
        m.put("msg", "文章修改成功");
        logger.info("获取的修改文章数据：" + article);
        /*return "article-update";*/
        return "redirect:/user/articles";
    }
     @RequestMapping("/main")
     public String myPage() {
         return "main"; // 返回逻辑视图名称
     }
    @GetMapping("/articles/{articleId}")
    public Article getArticleById(@PathVariable long articleId) {
        return articleMapper.getArticleById(articleId);
    }

    @PostMapping("/articles")
    public void addArticle(@RequestBody Article article) {
        articleMapper.insert(article);
    }

    @PutMapping("/articles/{articleId}")
    public void updateArticle(@PathVariable Integer articleId, @RequestBody Article article) {
        article.setArticleID(articleId);
        articleMapper.updateById(article);
    }

    @DeleteMapping("/articles/{articleId}")
    public void deleteArticle(@PathVariable Long articleId) {
        // 删除文章时，同时删除与年级的关联关系
        articleGradeMapper.deleteArticleGradeByArticleId(articleId);
        articleMapper.deleteById(articleId);
    }
    //删除
    @GetMapping("/delete")
    public String delete(Integer id,ModelMap m) {
        articleService.deleteArticle(id);
        m.put("msg", "文章删除成功");
        return "forward:/user/articles";
    }
    @GetMapping("/grades/")
    public List<Grade> getAllGrades() {
        return gradeMapper.getAllGrades();
    }

    @GetMapping("/grades/{gradeId}")
    public Grade getGradeById(@PathVariable Long gradeId) {
        return gradeMapper.selectById(gradeId);
    }

    @PostMapping("/grades")
    public void addGrade(@RequestBody Grade grade) {
        gradeMapper.insert(grade);
    }

    @PutMapping("/grades/{gradeId}")
    public void updateGrade(@PathVariable int gradeId, @RequestBody Grade grade) {
        grade.setGradeID(gradeId);
        gradeMapper.updateById(grade);
    }

    @DeleteMapping("/grades/{gradeId}")
    public void deleteGrade(@PathVariable Long gradeId) {
        gradeMapper.deleteById(gradeId);
    }

    @GetMapping("/grades/{gradeId}/articles")
    public List<Article> getArticlesByGradeId(@PathVariable Long gradeId) {
        return articleMapper.getArticlesByGradeId(gradeId);
    }

    @PostMapping("/articles/{articleId}/grades/{gradeId}")
    public void assignGradeToArticle(@PathVariable Long articleId, @PathVariable Long gradeId) {
        articleGradeMapper.addArticleGrade(articleId, gradeId);
    }

}
